﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;

#endregion

namespace Utils {
	/// <summary>
	/// This is a process chain manager. This class belongs to the process chain manager service.
	/// The functionality was extracted from the service to sepearte stuff.	
	/// </summary>
	public class ProcessChainManager {
		#region Public Properties

		#endregion

		#region Public Methods

		/// <summary>
		/// Register a new process step
		/// </summary>
		/// <typeparam name="TToHandle">type of the object</typeparam>
		/// <param name="area">process are</param>
		public void RegisterProcessStep(IProcessStep processStep) {
			m_processChains.Add(processStep);
		}

		/// <summary>
		/// Process the given object by searching the right chain and starting it
		/// </summary>
		/// <typeparam name="TToHandle">type of object</typeparam>
		/// <param name="area">process area</param>
		/// <param name="o">object to handle</param>
		/// <returns></returns>
		public Object Process<TToHandle>(TToHandle o) {
			Type key = typeof(TToHandle);

			foreach (ProcessStep<TToHandle> entry in m_processChains) {
				if (o is TToHandle) {
					if (entry.CanProcess(o)) {
						return entry.Process(o);
					}
				}
			}

			throw new NotSupportedException("no handler found, for handling: " + o);
		}

		/// <summary>
		/// returns all process chains of a client. a process chain is defined by its first step, 
		/// hence the return value of a list of process steps. 
		/// EACH STEP CORRESPONDS TO A DIFFERENT CHAIN.
		/// </summary>
		/// <param name="processChainId"></param>
		/// <returns></returns>
		public IList<IProcessStep> GetRegisteredProcesses() {
			return m_processChains;
		}

		#endregion

		#region Private Methods

		#endregion

		#region Private Fields

		/// <summary>
		/// List of process chains (each step is the root of one chain)
		/// </summary>
		private readonly List<IProcessStep> m_processChains = new List<IProcessStep>();

		#endregion
	}
}